package com.kucai.yifaqapp.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.update_PwdBean;
import com.kucai.yifaqapp.util.Md5;
import com.kucai.yifaqapp.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PasswordActivitySecond extends BaseActivitySimple {
    private RelativeLayout back;
    private LinearLayout skinbg;
    private EditText mima1;
    private EditText mima2;
    private EditText mima3;
    private Button conform;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_passwordsecond);
        back = findViewById(R.id.back);
        skinbg = findViewById(R.id.skinbg);
        mima1 = findViewById(R.id.mima1);
        mima2 = findViewById(R.id.mima2);
        mima3 = findViewById(R.id.mima3);
        conform = findViewById(R.id.conform);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mima1.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordActivitySecond.this, "请输入取款密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mima2.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordActivitySecond.this, "请输入新取款密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mima3.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordActivitySecond.this, "请输入新取款密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mima2.getText().toString().equals(mima3.getText().toString())) {
                    Toast.makeText(PasswordActivitySecond.this, "两次输入的密码请保持一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                conform();
            }
        });
    }

    private void conform() {
        HttpJsonUtils.getHttpApiInstance()
                .get_update_qkpwd(HttpAdress.getuseAdress() + "api/m/user/updateFundPwd",Info.user.token, Md5.toMd5(mima1.getText().toString()), Md5.toMd5(mima2.getText().toString()))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<update_PwdBean>(this) {
                    @Override
                    protected void onCustomNext(update_PwdBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                Toast.makeText(PasswordActivitySecond.this, "修改成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(PasswordActivitySecond.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }catch (Exception e){

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_password;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }
}
